<?php

namespace app\common\model\mysql;

use think\Model;

class InProduct extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'p_in_product';

    /**
     * 新增配送订单入库数据
     * @param $data
     * @return \think\Collection
     * @throws \Exception
     */
    public function addAllInProduct($data){
        return $this->saveAll($data);
    }

    /**
     * 查询入库商品仓库和仓位名称
     * @param $where
     * @param $field
     * @return mixed
     */
    public function getNormalInProductFind($where, $field){
        $result = $this->alias('a')->join('p_warehouse b', 'a.warehouse_id = b.warehouse_id')->join('p_warehouse_space c', 'a.warehouse_space_id = c.warehouse_space_id')->where($where)->field($field)->find();
        return $result ? $result->toArray() : [];
    }

    /**
     * 查询入库商品总量
     * @param array $where
     * @param string $field
     * @return float
     */
    public function getNormalInProductSum(array $where = [], string $field = "sellcount"){
        $result = $this->where($where)->sum($field);
        return $result;
    }

    /**
     * 查询库商品列表
     * @param $where
     * @param $field
     * @return mixed
     */
    public function getNormalInProductList($where, $field){
        $order = [
            "a.id" =>"asc",
        ];
        $result = $this->alias('a')->join('p_warehouse b', 'a.warehouse_id = b.warehouse_id')->join('p_warehouse_space c', 'a.warehouse_space_id = c.warehouse_space_id')->join('p_product d', 'a.product_id = d.product_id')->where($where)->field($field)->order($order)->select();
        return $result->toArray();
    }
}
